﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.TeamFoundation.Framework.Client;
using VisualWip.Core;
using VisualWip.Core.Interfaces;
using VisualWip.TFS2010Common;

namespace VisualWip.TFS2010PersistanceProvider
{
    public class TFS2010PathSelector : IPathSelector
    {
        private readonly TFSPropertyServiceHelper _propertyServiceHelper;

        public TFS2010PathSelector(TFSPropertyServiceHelper propertyServiceHelper)
        {
            _propertyServiceHelper = propertyServiceHelper;
        }

        #region Implementation of IPathSelector

        public PathSelectorResult OpenSelector()
        {
            var connection = new TfsConnection();
            connection.Connect("Select project to open settings from");

            var openDialog = new OpenDialog();
            var openDialogViewModel = new OpenDialogViewModel(GetAllPropertiesAsSettingsPathViews(connection));
            openDialog.DataContext = openDialogViewModel;
            openDialog.ShowDialog();

            if (openDialog.DialogResult == true && openDialogViewModel.SelectedSetting != null)
            {
                return new PathSelectorResult(true,
                                              new TFSConnectionSettingsPath
                                                  {
                                                      Path = openDialogViewModel.SelectedSetting.Name,
                                                      Connection = connection
                                                  });
            }

            return new PathSelectorResult(false, new NoSettingsPath());
        }

        public PathSelectorResult SaveSelector()
        {
            var connection = new TfsConnection();
            connection.Connect("Select project to save settings in");

            var saveDialog = new SaveDialog();
            var saveDialogViewModel = new SaveDialogViewModel(GetAllPropertiesAsSettingsPathViews(connection));
            saveDialog.DataContext = saveDialogViewModel;
            saveDialog.ShowDialog();

            if (saveDialog.DialogResult == true && saveDialogViewModel.Path != null)
            {
                return new PathSelectorResult(true,
                                              new TFSConnectionSettingsPath
                                                  {
                                                      Path = saveDialogViewModel.Path,
                                                      Connection = connection
                                                  });
            }

            return new PathSelectorResult(false, new NoSettingsPath());
        }

        #endregion

        public ObservableCollection<TFSConnectionSettingsPathView> GetAllPropertiesAsSettingsPathViews(
            TfsConnection connection)
        {
            ArtifactPropertyValue[] artifactPropertyValues = _propertyServiceHelper.GetAllProperties(connection);
            
            if (artifactPropertyValues.Length > 0)
            {
                IEnumerable<TFSConnectionSettingsPathView> settingsPathViews =
                    from artifactPropertyValue in artifactPropertyValues.FirstOrDefault().PropertyValues
                    select
                        new TFSConnectionSettingsPathView
                            {
                                IsSelected = false,
                                Name =
                                    artifactPropertyValue.PropertyName
                            };
                return new ObservableCollection<TFSConnectionSettingsPathView>(settingsPathViews);
            }
            return new ObservableCollection<TFSConnectionSettingsPathView>();
        }
    }
}